package com.dawn.framework.common.util.string;

import org.apache.commons.lang3.StringUtils;

/**
 * @description:
 * @author: cnsu-cmh
 * @time: 2024/8/7 13:31
 */
public class CodeUtils {

    /**
     * 用户姓名的打码隐藏加星号加*
     * 名字是两位时: 张三--> *三
     * 名字是三位及以上时: 燕双鹰 --> 燕*鹰
     *
     * @return 处理完成的姓名
     */
    public static String nameMask(String name){
        String res = name;
//        if (name.length() == 2){
//            res = name.replaceAll("^.", "*");
//        }else if (name.length() > 2){
//            res = name.replaceAll("(?<=.).(?=.)", "*");
//        }
        String mask = "";
        for (int i = 0; i < name.length()-1; i++) {
            mask += "*";
        }
        StringBuilder stringBuilder = new StringBuilder(name);
        res = stringBuilder.replace(0, name.length()-1, mask).toString();
        return res;
    }
    /**
     * 用户身份证号码的打码隐藏加星号加*
     *
     * @return 处理完成的身份证
     */
    public static String idCardMask(String idCardNum,int startIndex,int endIndex) {
        String res = idCardNum;
        if(endIndex > idCardNum.length()){
            return res;
        }
        if (!StringUtils.isEmpty(idCardNum)) {
            StringBuilder stringBuilder = new StringBuilder(idCardNum);
            res = stringBuilder.replace(startIndex, endIndex, "**********").toString();
        }
        return res;
    }
    /**
     * 用户电话号码的打码隐藏加星号加*
     *
     * @return 处理完成电话号码
     */
    public static String phoneMask(String phone,int startIndex,int endIndex) {
        String res = phone;
        if (!StringUtils.isEmpty(phone)) {
            StringBuilder stringBuilder = new StringBuilder(phone);
            res = stringBuilder.replace(startIndex, endIndex, "******").toString();
        }
        return res;
    }
}
